package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AdminWeekly;
import com.ruoyi.system.domain.AdminWeeklyJob;
import com.ruoyi.system.mapper.AdminWeeklyMapper;
import com.ruoyi.system.service.*;
import com.ruoyi.web.vo.AdminWeeklyStatisticView;
import com.ruoyi.web.vo.AdminWeeklyView;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 周报Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-01-20
 */
@Service
public class AdminWeeklyServiceImpl implements IAdminWeeklyService 
{
    private static final Logger log = LoggerFactory.getLogger(AdminDaliyServiceImpl.class);
    @Autowired
    private AdminWeeklyMapper adminWeeklyMapper;
    @Autowired
    private IAdminWeeklyJobService adminWeeklyJobService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysProjectService sysProjectService;
    @Autowired
    private ISysProductService sysProductService;


    /**
     * 查询周报
     * 
     * @param weeklyId 周报ID
     * @return 周报
     */
    @Override
    public AdminWeekly selectAdminWeeklyById(Long weeklyId)
    {
        AdminWeekly adminWeekly = adminWeeklyMapper.selectAdminWeeklyById(weeklyId);
        AdminWeeklyJob adminWeeklyJob = new AdminWeeklyJob();
        adminWeeklyJob.setWeeklyId(weeklyId);
        List<AdminWeeklyJob> list = adminWeeklyJobService.selectAdminWeeklyJobList(adminWeeklyJob);
        if (CollectionUtils.isNotEmpty(list)) {
            adminWeekly.setWeeklyJobs(list);
        } else {
            adminWeekly.setWeeklyJobs(new ArrayList<AdminWeeklyJob>());
        }
        return adminWeekly;
    }

    /**
     * 查询周报列表
     * 
     * @param vo 周报
     * @return 周报
     */
    @Override
    @DataScope(deptAlias = "e", userAlias = "b")
    public List<AdminWeeklyView> selectAdminWeeklyList(AdminWeeklyView vo)
    {
        if (null == vo.getUserId() && null == vo.getDeptId()) {
        } else {
            SysUser user = new SysUser();
            user.setDeptId(vo.getDeptId());
            user.setUserId(vo.getUserId());
            List<SysUser> userList = sysUserService.selectUserList(user);
            List<Long> userIds = new ArrayList<Long>();
            userIds.add(Long.valueOf(0));
            if (null != userList) {
                userList.stream().forEach(u ->{
                    userIds.add(u.getUserId());
                });
            }
            vo.setUserIds(userIds);
        }
        List<AdminWeeklyView> list = adminWeeklyMapper.selectAdminWeeklyListByView(vo);
        return list;
    }
    /**
     * 查询周报统计列表
     *
     * @param vo 周报统计
     * @return 周报统计
     */
    @Override
    public List<AdminWeeklyStatisticView> selectAdminWeeklyStatisticsList(AdminWeeklyView vo)
    {
        List<AdminWeeklyStatisticView> taskList = adminWeeklyJobService.selectAdminWeeklyJobStatisticsList(vo);
        return taskList;
    }

    /**
     * 新增周报
     * 
     * @param adminWeekly 周报
     * @return 结果
     */
    @Override
    public String insertAdminWeekly(AdminWeekly adminWeekly)
    {
        String message = null;
        Long userId = ShiroUtils.getUserId();
        adminWeekly.setUserId(userId);
        adminWeekly.setCreateTime(DateUtils.getNowDate());
        List<AdminWeeklyView> list = adminWeeklyMapper.selectAdminWeeklyList(adminWeekly);
        if (CollectionUtils.isNotEmpty(list)) {
            message = String.format("周报插入重复 WeeklyDateStart:{}, WeeklyDateStart:{}, userid:{}", adminWeekly.getWeeklyDateStart(), adminWeekly.getWeeklyDateEnd(), userId);
            log.warn(message);
            return message;
        }
        adminWeeklyMapper.insertAdminWeekly(adminWeekly);
        list = adminWeeklyMapper.selectAdminWeeklyList(adminWeekly);
        if (CollectionUtils.isNotEmpty(list) && list.size() != 1) {
            message = String.format("周报插入重复 WeeklyDateStart:{}, WeeklyDateStart:{}, userid:{},size{}", adminWeekly.getWeeklyDateStart(), adminWeekly.getWeeklyDateEnd(), userId, list.size());
            log.warn(message);
            return message;
        }
        Long weeklyId = list.get(0).getWeeklyId();
        for (AdminWeeklyJob job: adminWeekly.getWeeklyJobs()) {
            if (null == job.getProductType()) {
                continue;
            }
            job.setWeeklyId(weeklyId);
            adminWeeklyJobService.insertAdminWeeklyJob(job);
        }
        return null;
    }

    /**
     * 修改周报
     * 
     * @param adminWeekly 周报
     * @return 结果
     */
    @Override
    public int updateAdminWeekly(AdminWeekly adminWeekly)
    {
        for (AdminWeeklyJob adminWeeklyJob:adminWeekly.getWeeklyJobs()) {
            if (StringUtils.isBlank(adminWeeklyJob.getJobContent())) {
                continue;
            }
            adminWeeklyJob.setWeeklyId(adminWeekly.getWeeklyId());
            if (null != adminWeeklyJob.getJobId() && 0 != adminWeeklyJob.getJobId()) {
                adminWeeklyJobService.updateAdminWeeklyJob(adminWeeklyJob);
            } else {
                adminWeeklyJobService.insertAdminWeeklyJob(adminWeeklyJob);
            }
        }
        return adminWeeklyMapper.updateAdminWeekly(adminWeekly);
    }

    /**
     * 删除周报对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAdminWeeklyByIds(String ids)
    {
        return adminWeeklyMapper.deleteAdminWeeklyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除周报信息
     * 
     * @param weeklyId 周报ID
     * @return 结果
     */
    @Override
    public int deleteAdminWeeklyById(String weeklyId)
    {
        return adminWeeklyMapper.deleteAdminWeeklyById(weeklyId);
    }

    /**
     * 判断long是否为非0
     * @return
     */
    private boolean longIsNotZero(Long num){
        if (null != num && 0L != num) {
            return true;
        } else {
            return false;
        }
    }
}
